package com.lemner.ke.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lemner.ke.R;
import com.lemner.ke.adapter.MyCollectionAdapter;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.BaseObjectBean;
import com.lemner.ke.base.BasePageBean;
import com.lemner.ke.bean.RecommendBean;
import com.lemner.ke.model.mine.GetEntityCollectModel;
import com.lemner.ke.model.mine.GetEntityReadRecordModel;
import com.lemner.ke.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

/**

* 作者：张恺

* 时间：2018/9/25

* 类描述：我的收藏/浏览记录-页面

*/

public class MyCollectionActivity extends BaseActivity {
    private RecyclerView rv_data;
    private int cur_page = 1;
    private int lastVisibleItemPosition;
    private Boolean hasNextPage = false;
    private List<RecommendBean> list = new ArrayList<>();
    private MyCollectionAdapter myCollectionAdapter;
    private int page;
    @Override
    public int getLayoutFile() {
        return R.layout.activity_my_collection;
    }

    @Override
    public void initView() {
        topBar.setTitle("我的收藏");
    }

    @Override
    public void initEvent() {
        rv_data = findViewById(R.id.rv_mycollection_data);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        page = bundle.getInt("page");
        if (page == 0) {
            topBar.setTitle("我的收藏");
        } else {
            topBar.setTitle("浏览记录");
        }
        rv_data.setLayoutManager(new LinearLayoutManager(this));
        rv_data.setHasFixedSize(true);
        myCollectionAdapter = new MyCollectionAdapter(this);
        rv_data.setAdapter(myCollectionAdapter);
        rv_data.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    if (hasNextPage) {
                        cur_page += 1;
                        updateData();
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = list.size() - 1;
            }
        });
        updateData();
    }

    @Override
    protected void initSetting() {

    }

    @Override
    public void onClick(View v) {

    }
    private void updateData() {
        if (page == 0) {//我的收藏页面
            new GetEntityCollectModel().getEntityCollect(SpUtils.getString(this, SpUtils.USERIDS), cur_page, new BaseListener<BaseObjectBean<BasePageBean<RecommendBean>>>() {
                @Override
                public void onResponse(BaseObjectBean<BasePageBean<RecommendBean>> bean) {
                    if (list == null || list.size() == 0) {
                        list = bean.getData().getList();
                        myCollectionAdapter.setList(list);
                    } else {
                        myCollectionAdapter.addList(bean.getData().getList());
                    }
                }

                @Override
                public void onFail(String msg) {
                    myCollectionAdapter.setList(list);
                }
            });
        } else {//浏览记录页面
            new GetEntityReadRecordModel().getEntityReadRecord(SpUtils.getString(this, SpUtils.USERIDS), cur_page, new BaseListener<BaseObjectBean<BasePageBean<RecommendBean>>>() {
                @Override
                public void onResponse(BaseObjectBean<BasePageBean<RecommendBean>> bean) {
                    if (list == null || list.size() == 0) {
                        list = bean.getData().getList();
                        myCollectionAdapter.setList(list);
                    } else {
                        myCollectionAdapter.addList(bean.getData().getList());
                    }
                }

                @Override
                public void onFail(String msg) {
                    myCollectionAdapter.setList(list);
                }
            });

        }
    }
}